import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: "/home"
  },
  {
    path:'/login',
    component:()=>import('../views/Login.vue')
  },
  {
    path:'/home',
    component:()=>import('../views/Home.vue'),
    redirect:'/welcome',
    children:[
      {
        path:'/welcome',
        component:()=>import('../views/Welcome'),
      },
      {
        path:'/users',
        component:()=>import('../views/Users'),
      },
      {
        path:'/roles',
        component:()=>import('../views/roles'),
      },
      {
        path:'/rights',
        component:()=>import('../views/rights'),
      },
      {
        path:'/goods',
        component:()=>import('../views/goods'),
      },
      {
        path:'/params',
        component:()=>import('../views/params'),
      },
      {
        path:'/categories',
        component:()=>import('../views/categories'),
      },
      {
        path:'/orders',
        component:()=>import('../views/orders'),
      },
      {
        path:'/reports',
        component:()=>import('../views/reports'),
      },
      
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由导航守卫
router.beforeEach((to, from, next) => { // 在路由发生跳转之前要做的事情
  if(to.path === '/login') { // 用户要访问登录页面
    next(); // 放行
  } else { // 访问的不是登录页面
    if(sessionStorage.token) { // 登录成功了
      next(); // 放行
    } else { // 如果没有登录，跳转到登录页面
      next('/login')
    }
  }
})

export default router
